// CampDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VolDia.h"
#include "CampDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern CVolDiaApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CCampDlg dialog


CCampDlg::CCampDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCampDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCampDlg)
	m_Path = _T("");
	//}}AFX_DATA_INIT
}


void CCampDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCampDlg)
	DDX_Control(pDX, IDC_ECHO, m_Echo);
	DDX_Text(pDX, IDC_PATH, m_Path);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCampDlg, CDialog)
	//{{AFX_MSG_MAP(CCampDlg)
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_BN_CLICKED(IDC_DISCONNECT, OnDisconnect)
	ON_BN_CLICKED(IDC_ACQUIRE, OnAcquire)
	ON_BN_CLICKED(IDC_VIEW, OnView)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCampDlg message handlers

void CCampDlg::OnOpen() 
{
	CFileDialog	fileXls(TRUE, _T("xls"),_T(""), NULL,_T("MS Excel file|*.xls|All files|*.*||"));
	if(fileXls.DoModal() == IDOK){
		m_Path.Empty();
		m_Path = fileXls.GetPathName();
		UpdateData(false);
	}		
}

void CCampDlg::OnConnect() 
{
	BOOL	res_b;

	UpdateData(true);
	if(!XlsActive){
		excelObj = new CXlsObj;
		res_b = excelObj->Init();
		if(res_b == false){
			wsprintf(msg, _T("Excel file initialization failed."));
			echo(true);
		}
		else{
			wsprintf(msg, _T("Excel file initialization completed."));
			echo(true);
			XlsActive = true;
		}

		//res_b = excelObj->OpenWorkBook(m_Path, 0, VARIANT_TRUE, excelInst);
		res_b = (int)excelObj->OpenWorkBook(m_Path, VARIANT_TRUE, excelInst);
		if(res_b == false){
			wsprintf(msg, _T("Excel file open failed."));
			echo(true);
		}
		else{
			wsprintf(msg, _T("Excel file open completed."));
			echo(true);
		}
	}
	else{
		wsprintf(msg, _T("Xls document %s already opened."), m_Path);
		echo(true);
	}
	
	wsprintf(msg, _T("sp"));
	echo(true);	
}

void CCampDlg::echo(BOOL type){
	SYSTEMTIME	timeStamp;
	TCHAR		timeStr[100];
	TCHAR		*temp;
	
	if(_tcscmp(msg, _T("sp"))!=0){
		GetSystemTime(&timeStamp);
		_stprintf_s(timeStr, _T("[hh%02d  mm%02d  ss%02d,%03d  (GMT)]"), timeStamp.wHour, timeStamp.wMinute, timeStamp.wSecond, timeStamp.wMilliseconds);
		temp = new TCHAR[sizeof(timeStamp) + sizeof(msg)];
		_stprintf_s(temp,sizeof(temp), _T("%s     %s"), timeStr, msg);
		m_Echo.InsertString(m_Echo.GetCount(), temp);
		Sleep(100);
		UpdateData(true);
	}
	else{
		temp = new TCHAR[223];
		_stprintf_s(temp,sizeof(temp), _T("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"));
		m_Echo.InsertString(m_Echo.GetCount(), temp);
		Sleep(100);
		UpdateData(true);
	}
	delete temp;
}

void CCampDlg::OnDisconnect() 
{
	int res_b;
	if(XlsActive){
		res_b = excelObj->Close();
		if(res_b == false){
			_stprintf_s(msg, _T("Error closing Excel document."));
			echo(true);
		}
		else{
			_stprintf_s(msg, _T("Excel document closed."));
			echo(true);
			XlsActive = false;
		}
	}	
}

void CCampDlg::OnCancel() 
{
	CCampDlg::OnDisconnect();	
	CDialog::OnCancel();
}

BOOL CCampDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	m_Path = XLS_DEFAULT_PATH;
	XlsActive = false;
	UpdateData(false);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CCampDlg::OnAcquire() 
{
	RETCOND		retCnd;
	RETSTAT		retSt;
	FILEIDX		filePhas;
	int			opNum;
	phas_struc	phas;

	opNum = -1;
	filePhas.num = F_PHAS;

	if(!XlsActive){
		_stprintf_s(msg, _T("Unable to acquire no Excel document opened."));
		echo(true);
	}

	opNum = AcquirePhas(excelObj, EXCEL_SHEET, this->m_hWnd);
	if(opNum == 0){
		_stprintf_s(msg, _T("Technological cycles data acquisition failed check log file."));
		echo(true);
		_stprintf_s(msg, _T("sp"));
		echo(true);
	}
	else{
		_stprintf_s(msg, _T("%d Technological cycles acquired successfully."), opNum);
		theApp.m_pDlg->m_pPhaseExecDialog->EchoStrAdd(msg);
		echo(true);
		_stprintf_s(msg, _T("sp"));
		echo(true);
		retCnd = FileRead(&filePhas, 1, 1, &phas, sizeof(phas));
		if(retCnd != R_NORMAL){

		}
		retSt = SetDynNextPhase(phas.phase);
		if(retSt != RT_NORMAL){

		}
		retSt = SetDynCurrentPhase(0);
		if(retSt != RT_NORMAL){

		}
	}

}

void CCampDlg::OnView() 
{
	RETCOND		retCnd;
	FILEIDX		filePhas;
	phas_struc	phas;
	int			i;
	
	filePhas.num = F_PHAS;

	_stprintf_s(msg, _T("sp"));
	echo(true);

	for(i = 1; i < N_MAX_PHASE; i++){
		retCnd = FileRead(&filePhas, i, 1, &phas, sizeof(phas));
		if(retCnd != R_NORMAL){
			_stprintf_s(msg, _T("Error reding DAFS file F.PHAS record %d"), i);
			echo(true);
			_stprintf_s(msg, _T("sp"));
			echo(true);
			break;
		}
		if(phas.phase != 0){
			_stprintf_s(msg, _T("[Op. %3d] -- [%s] -- [%s] -- [%s] -- [%3d,%3d] -- [%3d,%3d]"), phas.phase, phas.cmd_type, phas.obj_type, phas.object, phas.SV_X, phas.SV_Y, phas.DV_X, phas.DV_Y );
			m_Echo.InsertString(m_Echo.GetCount(), msg);
			UpdateData(true);
		}
	}
}
